using Game.Const;
using Game.Core;
using Game.Main.Model;
using Game.Manager;
using Game.XH;
using UnityEngine;

namespace Game.Component
{
	public class ScissorItem : BaseItem
	{
		private GameObject targetObject;

		private BaseItem targetItem;

		private float stayTime;

		private int minHairId;

		private int maxHairId;

		private bool playAnim;

		private float animTimer;

		private Transform leftScissor;

		private Transform rightScissor;

		public override void Init(ItemArchiveData data)
		{
			base.Init(data);
			string[] array = base.itemData.scriptParams.Split(',');
			minHairId = int.Parse(array[0]);
			maxHairId = int.Parse(array[1]);
			leftScissor = model.Find("Left");
			rightScissor = model.Find("Right");
			rightScissor.SetLocalZ(1E-05f);
		}

		protected override void LateUpdate()
		{
			base.LateUpdate();
			if (targetItem != null)
			{
				stayTime += Time.deltaTime;
				if (stayTime > 0.6f)
				{
					int hairId = ((CharacterItem)targetItem).GetHairId();
					((CharacterItem)targetItem).SetHair(GetRandomId(hairId));
					Singleton<AudioManager>.Instance.PlaySound("sound_explode");
					Singleton<EffectManager>.Instance.ShowEffect(PathConst.EFFECT + "efc_explode_1", targetItem.transform.position + new Vector3(0f, 2.8f, -0.0004f), 1f);
					Singleton<EffectManager>.Instance.ShowEffect(PathConst.EFFECT + "efc_blink_1", targetItem.transform.position + new Vector3(0f, 2.8f, -0.0004f), 3f);
					targetObject = null;
					targetItem = null;
				}
			}
			if (playAnim)
			{
				animTimer += Time.deltaTime;
				if (animTimer <= 0.2f)
				{
					SetScissorAngle(20f * (animTimer / 0.2f));
				}
				else if (animTimer <= 0.4f)
				{
					SetScissorAngle(20f - 20f * ((animTimer - 0.2f) / 0.2f));
				}
				else
				{
					animTimer = 0f;
				}
			}
		}

		public override void TouchDown(TouchEventData touchEventData)
		{
			base.TouchDown(touchEventData);
			if (isDrag)
			{
				playAnim = true;
				animTimer = 0f;
			}
		}

		public override void TouchUp(TouchEventData touchEventData)
		{
			targetObject = null;
			targetItem = null;
			stayTime = 0f;
			playAnim = false;
			SetScissorAngle(0f);
			base.TouchUp(touchEventData);
		}

		private void SetScissorAngle(float angle)
		{
			leftScissor.localEulerAngles = new Vector3(0f, 0f, 0f - angle);
			rightScissor.localEulerAngles = new Vector3(0f, 0f, angle);
		}

		private int GetRandomId(int id)
		{
			if (maxHairId - minHairId < 2)
			{
				Trace.LogWarning("剪刀未配置可剪发型：" + base.itemData.id);
				return 0;
			}
			int num;
			for (num = Random.Range(minHairId, maxHairId + 1); num == id; num = Random.Range(minHairId, maxHairId + 1))
			{
			}
			return num;
		}

		protected override void OnItemCompTriggerEnter(ColliderData collData)
		{
			base.OnItemCompTriggerEnter(collData);
			if (targetObject == null && collData.trigger.type == ItemComponentTrigger.Type.Head)
			{
				targetObject = collData.coll.gameObject;
				targetItem = collData.trigger.owner;
				stayTime = 0f;
			}
		}

		protected override void OnItemCompTriggerExit(ColliderData collData)
		{
			base.OnItemCompTriggerExit(collData);
			if (targetObject != null && collData.coll.gameObject == targetObject)
			{
				targetObject = null;
				targetItem = null;
				stayTime = 0f;
			}
		}
	}
}
